<?php

if(!defined('IN_DISCUZ')) {
	exit('Access Denied');
}

require_once  './source/class/bid/class_bid.php';
require_once libfile('function/core');
require_once libfile('function/database');

$bid = bid::instance();
$activities = $bid->get_vote_activity_list();


$page =  getgpc('page');
if (empty($page)) { $page =  1; } else { $page = intval($page); }
if($page < 1) { $page=1; }
$pagesize = $bid->bid_data->pagesize;;
$start = ($page-1) * $pagesize;

$aids = get_column_list($activities, 'id');
for($i = 0, $n = count($activities); $i < $n; $i++ ) {
	$aid = $activities[$i]['id'];
	$activity = $activities[$i];
	$total_score = build_total_score_sql($activity['credits_rule']);
	$rank_sql = ' select  '.$total_score.' as total_score, uc.* from '.DB::table('vote_user_count').' uc ';
	$sql = ' select @rownum := @rownum + 1 as rank, a.* from ( '.$rank_sql.' ) a,(select @rownum := '.$start.') r
		where a.aid = '.$aid.' order by a.total_score desc limit '.$start.','.$perpage;
	$users = query($sql);
	get_vote_user($users);
	get_user_pic($users);

	$count = getcount('vote_user', array('aid' => $aid));
	$multi = multi($count, $perpage, $page, "bid.php?mod=list&aid=$aid");

	$activities[$i]['users'] = $users;
	$activities[$i]['multi'] = $multi;
}

get_activities_count($activities);

// 如果用户已经登录，检查用户可以报名参加那些活动。
if ($_G['uid']) {
	$sql = ' select u.aid, a.bid_start_day, a.bid_end_day from '.DB::table('vote_user').' u, '.DB::table('vote_activity').'
		a where u.uid = '.$_G['uid'].' and u.aid = a.id ';
	$entry_activities = query($sql);
	$entry_status = array();
	foreach ($activities as $activity){
		$entry_status[$activity['id']] = check_entry_status($activity, $entry_activities);
	}
}

include_once template("bid/list");
?>